import { createRouter, createWebHistory } from 'vue-router';
import SearchResult from "@/views/index/SearchResult.vue";
import NovelDetail from '../views/index/NovelDetail.vue';
import Home from '../views/index/Home.vue'; // 根据项目结构调整路径
import ChapterList from '../views/index/ChapterList.vue'; // 章节目录列表组件
import Chapter from "@/views/index/Chapter.vue";
import AllNovel from "@/views/index/AllNovel.vue";
import UserLogin from "@/views/user/UserLogin.vue";
import UserCenter from "@/views/user/UserCenter.vue";
import App from "@/App.vue";
import UserReg from "@/views/user/UserReg.vue";
import Writer from "@/views/user/Writer.vue";
import EditNovel from "@/views/user/EditNovel.vue";
import EditChapterList from "@/views/user/EditChapterList.vue";
import EditChapter from "@/views/user/EditChapter.vue";
import addChapter from "@/views/user/addChapter.vue";
import AdminLogin from "@/views/admin/AdminLogin.vue";
import AdminIndex from "@/views/admin/AdminIndex.vue";
import AdminUser from "@/views/admin/AdminUser.vue";
import AdminNovel from "@/views/admin/AdminNovel.vue";
import AdminChapter from "@/views/admin/AdminChapter.vue";
import AdminAddUser from "@/views/admin/AdminAddUser.vue";
import AdminEditUser from "@/views/admin/AdminEditUser.vue";
import AdminAddNovel from "@/views/admin/AdminAddNovel.vue";
import AdminEditNovel from "@/views/admin/AdminEditNovel.vue";
import AdminSearchChapter from "@/views/admin/AdminSearchChapter.vue";
import AdminEditChapter from "@/views/admin/AdminEditChapter.vue";
import AdminAddChapter from "@/views/admin/AdminAddChapter.vue";

const routes = [
    {
        path: '/', // 首页路由
        name: 'Home',
        component: Home,
        meta: { title: '首页' } // 添加meta字段，定义标题
    },
    {
        path: '/novel/:novelId', // 动态路由
        name: 'NovelDetail',
        component: NovelDetail,
        meta: { title: '小说详情' } // 添加meta字段，定义标题
    },
    {
        path: '/so/:query', // 搜索结果路径
        name: 'SearchResult',
        component: SearchResult,
    },
    {
        path: '/chapter/:chapterId', // 路径包含小说 ID
        name: 'Chapter',
        component: Chapter // 对应的组件
    },
    {
        path: '/all-novels/:page', // 路径包含page
        name: 'AllNovel',
        component: AllNovel // 对应的组件
    },
    {
        path: '/user/login', // 路径包含page
        name: 'UserLogin',
        component: UserLogin // 对应的组件
    },
    {
        path: '/user/reg', // 路径包含page
        name: 'UserReg',
        component: UserReg // 对应的组件
    },
    {
        path: '/user/center', // 路径包含page
        name: 'UserCenter',
        component: UserCenter // 对应的组件
    },
    {
        path: '/writer', // 路径包含page
        name: 'Writer',
        component: Writer // 对应的组件
    },
    {
        path: '/edit/novel/:novelId', // 路径包含page
        name: 'EditNovel',
        component: EditNovel // 对应的组件
    },
    {
        path: '/edit/chapter/:chapterId', // 路径包含page
        name: 'EditChapter',
        component: EditChapter // 对应的组件
    },
    {
        path: '/edit/chapterlist/:novelId', // 路径包含page
        name: 'EditChapterList',
        component: EditChapterList // 对应的组件
    },
    {
        path: '/add/chapter/:novelId', // 路径包含page
        name: 'addChapter',
        component: addChapter // 对应的组件
    },
    {
        path: '/admin/login', // 路径包含page
        name: 'AdminLogin',
        component: AdminLogin // 对应的组件
    },
    {
        path: '/admin/index', // 路径包含page
        name: 'AdminIndex',
        component: AdminIndex // 对应的组件
    },
    {
        path: '/admin/user', // 路径包含page
        name: 'AdminUser',
        component: AdminUser // 对应的组件
    },
    {
        path: '/admin/novel', // 路径包含page
        name: 'AdminNovel',
        component: AdminNovel // 对应的组件
    },
    {
        path: '/admin/chapter', // 路径包含page
        name: 'AdminChapter',
        component: AdminChapter // 对应的组件
    },
    {
        path: '/admin/chapter/search/:novelId', // 路径包含page
        name: 'AdminSearchChapter',
        component: AdminSearchChapter // 对应的组件
    },
    {
        path: '/admin/chapter/edit/:chapterId', // 路径包含page
        name: 'AdminEditChapter',
        component: AdminEditChapter // 对应的组件
    },
    {
        path: '/admin/chapter/add/:novelId', // 路径包含page
        name: 'AdminAddChapter',
        component: AdminAddChapter // 对应的组件
    },
    {
        path: '/admin/adduser', // 路径包含page
        name: 'AdminAddUser',
        component: AdminAddUser // 对应的组件
    },
    {
        path: '/admin/edituser/:userId', // 路径包含page
        name: 'AdminEditUser',
        component: AdminEditUser // 对应的组件
    },
    {
        path: '/admin/addnovel/', // 路径包含page
        name: 'AdminAddNovel',
        component: AdminAddNovel // 对应的组件
    },
    {
        path: '/admin/editnovel/:novelId', // 路径包含page
        name: 'AdminEditNovel',
        component: AdminEditNovel // 对应的组件
    },
    {
        path: '/:catchAll(.*)', // 通配符路由，用于处理未知路由
        redirect: '/', // 重定向到首页
    },
];


const router = createRouter({
    history: createWebHistory(), // 使用 HTML5 历史模式
    routes,
});
/*router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = document.title + ' - ' + to.meta.title;
    }
    next();
});*/
export default router; // 导出路由配置
